package com.example.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

 class CalendarUtil {
    
    //���µ�һ��
    public static Date getNextMonth(Date beginTime) {
        Calendar c = Calendar.getInstance();
        c.setTime(beginTime);
        c.set(Calendar.MONTH, c.get(Calendar.MONTH) + 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return c.getTime();
    }

    //���켸��
    public static int getMonthOfDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_MONTH);
    }
    //�����ж�����
    public static int getLastDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.getActualMaximum(Calendar.DATE);
        return month;
    }
   
    //��ǰ�� -1
    public static int getMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        return month;
    }
  
    //���µ�һ��
      public static Date getFirstDayOfMonth(Date beginTime){
           Calendar c = Calendar.getInstance();
           c.setTime(beginTime);
           c.set(Calendar.DAY_OF_MONTH, 1);
           return c.getTime();
      }
  
    //��õ��ܵڼ���  i=0��ʾ����һ
    public static String getNowWeekBegin(Date date,int i) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        cd.set(Calendar.DAY_OF_WEEK, Calendar.MONTH +i);
        if(i==6){
         cd.set(Calendar.DAY_OF_WEEK, Calendar.MONTH+5);
         cd.add(cd.DATE, 1); 
        }
        Date  monday =cd.getTime();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        String preMonday = df.format(monday);
        return preMonday ;
        } 
    
    //��õ��ܵڼ��죬���������Ρ�i=0��ʾ����һ
    public static Date getNowWeekBeginDate(Date date,int i) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        cd.set(Calendar.DAY_OF_WEEK, Calendar.MONTH +i);
        if(i==6){
         cd.set(Calendar.DAY_OF_WEEK, Calendar.MONTH+5);
         cd.add(cd.DATE, 1); 
        }
        Date  monday =cd.getTime();
        return monday ;
        } 
    //��ñ�������
    public static List<String> getDaysOfWeek(Date date){
        List<String> days = new ArrayList<String>();
        for (int i = 0; i < 7; i++) {
            days.add(getNowWeekBegin(date,i));
        }
        return days;
    }
    //��ñ�������
    public static List<Date> getDaysOfWeekDate(Date date){
        List<Date> days = new ArrayList<Date>();
        for (int i = 0; i < 7; i++) {
            days.add(getNowWeekBeginDate(date,i));
        }
        return days;
    }
    
    /**
     * ��õ��µڼ���
     * @param date
     * @param week
     * @return
     */
    public static List<String> getWeeksOfMonth(Date date,int week){
        int day =7*week+1;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, day);
        return getDaysOfWeek(c.getTime());
    }
  
    //�������һ��
    public static Date getDateOfLastDayOfMonth(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DATE));
        return c.getTime();
    }
    //��ñ��µڼ���,0��ʾ��һ��
   public static Date getDayOfMonth(Date date,int i){
       Calendar c = Calendar.getInstance();
           c.setTime(date);
           c.set(Calendar.DAY_OF_MONTH, i+1);
       return c.getTime();
   }
    //���¹�����
    public static int getWeekyofMonth(Date date){
        Date d1 = getFirstDayOfMonth(date);//��һ��
        Date d2 = getDateOfLastDayOfMonth(date);//���һ��
        Date mon1 =getNowWeekBeginDate(d1,0);//��һ������������һ
        Date mon2 =getNowWeekBeginDate(d2,0);//���һ������������һ
        return (int) ((mon2.getTime()-mon1.getTime())/3600/24/1000)/7;
    }
   public static List<String> getWeekName(){
       List<String> list = new ArrayList<String>();
       list.add("��һ");
       list.add("�ܶ�");
       list.add("����");
       list.add("����");
       list.add("����");
       list.add("����");
       list.add("����");
       return list;
       
   }
   /**
    * ���Ȼ�� ��һ��ʱ�䣬��2014-07-14Ϊ����
    * Ȼ���ô��������ڵ�����һ�����ʱ�����7.
    * Ȼ��������2�����������0�����ǵ��ܣ�������˫��
    * @param date
    * @return
    */
 public static String getDanShuangWeek(Date date){
    Date beginDate =StringToDate("2014-07-14");
    Date nowDate =getNowWeekBeginDate(date,0); 
    int temp = (int) ((nowDate.getTime() -beginDate.getTime())/3600/24/1000);
    int danshuang=temp%2;
    String str=null;
    if(danshuang==0){
        str="����";
    }else{
        str="˫��";
    }
     return str;
 }
 
 public static int getDanShuangWeekInt(Date date){
        Date beginDate =StringToDate("2014-07-14");
        
        Date nowDate =getNowWeekBeginDate(date,0); 
        int temp = (int) ((nowDate.getTime() -beginDate.getTime())/3600/24/1000);
        int danshuang=temp%2;
         return danshuang;
     }
 
 public static Date StringToDate(String strDate){
     SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
     Date date =null;
     try {
          date = df.parse(strDate);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     return date;
 }
 
 public static Date DateToDate(Date date){
     SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
     String strDate= df.format(date);
     return StringToDate(strDate);
 }
 
// public static void main(String[] args) {
////    System.out.println(getDanShuangWeek(StringToDate("2014-08-02"))); ;
//    // System.out.println(getDayOfMonth(new Date(),10));
//     System.out.println(DateToDate(new Date()));
//}
}